package com.congee02.ms;

import java.util.Arrays;

public class MaxWidthRamp_WTF {

    public int maxWidthRamp(int[] nums) {

        int n = nums.length;

        int[] sta = new int[n];
        int top = -1;

        for (int i = 0 ; i < n ; i ++ ) {
            if (top == -1 || nums[i] < nums[sta[top]]) {
                sta[++ top] = i;
            }
        }

        int ans = 0;
        for (int i = n - 1 ; i >= 0 ; i -- ) {
            while (top != -1 && nums[i] > nums[sta[top]]) {
                ans = Math.max(ans, i - sta[top --]);
            }
        }

        return ans;
    }

}
